package org.microframework.cloud.logs.controller;

import org.microframework.base.core.domain.R;
import org.microframework.base.core.web.controller.BaseController;
import org.microframework.base.core.web.page.PageQuery;
import org.microframework.cloud.logs.model.domain.ApiLog;
import org.microframework.cloud.logs.model.dto.ApiLogDTO;
import org.microframework.cloud.logs.model.dto.ApiLogQueryDTO;
import org.microframework.cloud.logs.model.dto.ApiLogStatsDTO;
import org.microframework.cloud.logs.service.IApiLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jfinal.plugin.activerecord.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;

/**
 * API调用日志控制器
 *
 * @author zs
 * @version 1.0.0
 * @since 2023-08-01
 * @description API调用日志控制器，提供API调用日志的查询、删除等管理功能
 */
@RestController
@RequestMapping("/apiLog")
@Tag(name = "API调用日志", description = "API调用日志管理接口，提供日志查询、删除、导出和统计等功能")
public class ApiLogController extends BaseController {

	@Autowired
	private IApiLogService apiLogService;

	@GetMapping("/pages")
	@Operation(summary = "查询API调用日志列表", description = "分页查询API调用日志数据，支持多条件筛选和排序")
	@Parameters({
		@Parameter(name = "query.serviceName", description = "服务名称", schema = @Schema(type = "string")),
		@Parameter(name = "query.apiPath", description = "API路径", schema = @Schema(type = "string")),
		@Parameter(name = "query.method", description = "请求方法", schema = @Schema(type = "string", allowableValues = {"GET", "POST", "PUT", "DELETE"})),
		@Parameter(name = "query.status", description = "响应状态码", schema = @Schema(type = "integer")),
		@Parameter(name = "query.startTime", description = "开始时间", schema = @Schema(type = "string", format = "date-time")),
		@Parameter(name = "query.endTime", description = "结束时间", schema = @Schema(type = "string", format = "date-time")),
		@Parameter(name = "pageNumber", description = "当前页码，从1开始", schema = @Schema(type = "integer", defaultValue = "1")),
		@Parameter(name = "pageSize", description = "每页记录数", schema = @Schema(type = "integer", defaultValue = "10")),
		@Parameter(name = "orderBy", description = "排序字段", schema = @Schema(type = "string", allowableValues = {"createTime", "responseTime", "status"})),
		@Parameter(name = "orderDirection", description = "排序方向", schema = @Schema(type = "string", allowableValues = {"asc", "desc"}))
	})
	@ApiResponses({
		@ApiResponse(responseCode = "200", description = "查询成功"),
		@ApiResponse(responseCode = "400", description = "请求参数错误"),
		@ApiResponse(responseCode = "500", description = "服务器内部错误")
	})
	public R<Page<ApiLogDTO>> pages(PageQuery<ApiLog> pageQuery) {
		return R.ok(apiLogService.selectApiLogList(pageQuery));
	}

	/**
	 * 获取API调用日志详细信息
	 */
	@GetMapping("/{id}")
	@Operation(summary = "获取API调用日志详情", description = "根据ID查询API调用日志的详细信息")
	@Parameter(name = "id", description = "日志ID", required = true, schema = @Schema(type = "string", format = "uuid"))
	@ApiResponses({
		@ApiResponse(responseCode = "200", description = "查询成功"),
		@ApiResponse(responseCode = "404", description = "日志不存在"),
		@ApiResponse(responseCode = "500", description = "服务器内部错误")
	})
	public R<ApiLogDTO> getInfo(@PathVariable("id") String id) {
		return R.ok(apiLogService.selectApiLogById(id));
	}

	/**
	 * 删除API调用日志
	 */
	@DeleteMapping("/{ids}")
	@Operation(summary = "删除API调用日志", description = "根据ID批量删除API调用日志，支持多个ID以逗号分隔")
	@Parameter(name = "ids", description = "日志ID，多个以逗号分隔", required = true, schema = @Schema(type = "string", example = "1,2,3"))
	@ApiResponses({
		@ApiResponse(responseCode = "200", description = "删除成功"),
		@ApiResponse(responseCode = "400", description = "请求参数错误"),
		@ApiResponse(responseCode = "500", description = "服务器内部错误")
	})
	public R<Integer> remove(@PathVariable String ids) {
		return R.ok(apiLogService.deleteApiLogByIds(ids));
	}

	/**
	 * 清空API调用日志
	 */
	@DeleteMapping("/clean")
	@Operation(summary = "清空API调用日志", description = "清空所有API调用日志数据，此操作不可恢复")
	@ApiResponses({
		@ApiResponse(responseCode = "200", description = "清空成功"),
		@ApiResponse(responseCode = "500", description = "服务器内部错误")
	})
	public R<Boolean> clean() {
		return R.ok(apiLogService.cleanApiLog());
	}

	/**
	 * 导出API调用日志
	 */
	@PostMapping("/export")
	@Operation(summary = "导出API调用日志", description = "根据查询条件导出API调用日志数据，支持Excel格式")
	@io.swagger.v3.oas.annotations.parameters.RequestBody(
		description = "导出查询条件",
		required = true,
		content = @io.swagger.v3.oas.annotations.media.Content(
			schema = @Schema(implementation = ApiLogQueryDTO.class)
		)
	)
	@ApiResponses({
		@ApiResponse(responseCode = "200", description = "导出成功"),
		@ApiResponse(responseCode = "400", description = "请求参数错误"),
		@ApiResponse(responseCode = "500", description = "服务器内部错误")
	})
	public R<Object> export(ApiLogQueryDTO queryDTO) {
		return R.ok(apiLogService.exportApiLog(queryDTO));
	}

	/**
	 * 查询API调用统计数据
	 */
	@GetMapping("/stats")
	@Operation(summary = "查询API调用统计", description = "获取API调用统计数据，包括调用次数、成功率、平均响应时间等指标")
	@ApiResponses({
		@ApiResponse(responseCode = "200", description = "查询成功"),
		@ApiResponse(responseCode = "500", description = "服务器内部错误")
	})
	public R<ApiLogStatsDTO> getApiStats() {
		return R.ok(apiLogService.getApiCallStatistics());
	}
}